﻿@page "/table"
@using System.Diagnostics.CodeAnalysis;
@attribute [TabItemOption(Text = "Table")]
<PageTitle>Table</PageTitle>

<Table TItem="Foo" @ref="TableInstance"
       ShowToolbar="true">
    <TableColumns>
        <TableColumn @bind-Field="@context.Name" />
        <TableColumn @bind-Field="@context.Address" />
@*         <TableColumn @bind-Field="@context.Complete" Filterable="true" Items="@IsBoolFilterItems" />
        <TableColumn @bind-Field="@context.Complete" Filterable="true">
            <FilterTemplate>
                <BoolFilter Items="@IsBoolFilterItems" />
            </FilterTemplate>
        </TableColumn>
 *@        <TableColumn @bind-Field="@context.Complete" Filterable="true">
            <FilterTemplate>
                <MyBoolFilter />
            </FilterTemplate>
        </TableColumn>
    </TableColumns>
</Table>

@code {

    [NotNull]
    private Table<Foo>? TableInstance { get; set; }


    private IEnumerable<SelectedItem> IsBoolFilterItems { get; set; } = new List<SelectedItem>()
    {
        new SelectedItem(){Value="",Text="全部"},
        new SelectedItem(){Value="true",Text="是的"},
        new SelectedItem(){Value="false",Text="不是"}
    };

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        // await SetDefaultFilter();
    }

    private async Task SetDefaultFilter()
    {
        var column = TableInstance.Columns.First(x => x.GetFieldName() == nameof(Foo.Complete));
        var filters = new FilterKeyValueAction()
            {
                Filters = new()
                    {
                        new FilterKeyValueAction {FieldKey=nameof(Foo.Complete), FieldValue = false, FilterAction = FilterAction.Equal }
                    }
            };
        var filterAction = column.Filter?.FilterAction;
        if (filterAction != null)
        {
            await filterAction.SetFilterConditionsAsync(filters);
        }
    }
}